/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Facades;

import VOs.Enterprise;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

/**
 *
 * @author Julian
 */
@Stateless
public class EnterpriseFacade implements EnterpriseFacadeRemote {
    @PersistenceContext(unitName = "AccesoDatosPU")
    private EntityManager em;

    public void create(Enterprise enterprise) throws Exception{
        try {
        em.persist(enterprise);
        } catch (Exception e) {
            throw e;
        }
        
    }

    public void edit(Enterprise enterprise) throws Exception{
        try {
        em.merge(enterprise);
        } catch (Exception e) {
            throw e;
        }
        
    }

    public void remove(Enterprise enterprise) throws Exception{
        try {
        em.remove(em.merge(enterprise));
        } catch (Exception e) {
            throw e;
        }
        
    }

    public Enterprise find(Object id) throws Exception{
        try {
        return em.find(Enterprise.class, id);
        } catch (Exception e) {
            throw e;
        }
        
    }

    public List<Enterprise> findAll() throws Exception{
        try {
        return em.createQuery("select object(o) from Enterprise as o").getResultList();
        } catch (Exception e) {
            throw e;
        }
        
    }

    public List<Enterprise> findRange(int[] range) {
        Query q = em.createQuery("select object(o) from Enterprise as o");
        q.setMaxResults(range[1] - range[0]);
        q.setFirstResult(range[0]);
        return q.getResultList();
    }

    public int count() {
        return ((Long) em.createQuery("select count(o) from Enterprise as o").getSingleResult()).intValue();
    }

    public Enterprise findid(String id) throws Exception
    {
        try {
        Query query = em.createNamedQuery("Enterprise.findById");
       query.setParameter("id", id);

        return (Enterprise) query.getSingleResult();
        } catch (Exception e) {
            throw e;
        }
        
    }

}
